#!/usr/bin/env python3
# ==============================================================================================
# BSD 3-Clause Clear License
# Copyright © 2025 ZAMA. All rights reserved.
# ----------------------------------------------------------------------------------------------
#  Script used to create a ntt_core_gf64_common_definition_pkg.sv file.
# ==============================================================================================

import os       # OS functions
import sys      # manage errors
import argparse # parse input argument
import pathlib  # Get current file path
import jinja2

TEMPLATE_NAME = "ntt_core_common_cut_definition_pkg.sv.j2"

#=====================================================
# Main
#=====================================================
if __name__ == '__main__':

#=====================================================
# Parse input arguments
#=====================================================
    parser = argparse.ArgumentParser(description = "Create a module directory structure.")
    parser.add_argument('-J',   dest='col_l',   type=int, help="Number of stages for each radix column.", default=[], action='append')
    parser.add_argument('-o',   dest='outfile', type=str, help="Output filename.", required=True)
    parser.add_argument('-f',   dest='force',   help="Overwrite if file already exists", action="store_true", default=False)

    args = parser.parse_args()

#=====================================================
# Create files
#=====================================================
    template_path   = os.path.join(pathlib.Path(__file__).parent.absolute(), "templates")
    template_loader = jinja2.FileSystemLoader(searchpath=template_path)
    template_env    = jinja2.Environment(loader=template_loader)

    # check parameters
    if (len(args.col_l) == 0):
        sys.exit("ERROR> No radix column description given!.")

    col_l = args.col_l
    col_l_s = ""
    for i in args.col_l:
      col_l_s = f"32'd{i}," + col_l_s
    col_l_s = "{"+col_l_s[:-1]+"}"

    config = {"col_nb" : len(col_l),
              "col_l"  : col_l_s}

    template = template_env.get_template(TEMPLATE_NAME)
    file_path = args.outfile
    if (os.path.exists(file_path) and not(args.force)):
        sys.exit("ERROR> File {:s} already exists".format(file_path))
    else:
        if (os.path.exists(file_path)):
            print("INFO> File {:s} already exists. Overwrite it.".format(file_path))
        with open(file_path, 'w') as fp:
            fp.write(template.render(config))



